<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@include file="connection.jsp"%>
<%@ page import ="util.MailUtility"%>
<%
            Connection c = getAdminConnection();
            Statement s = c.createStatement();
			Statement s2 = c.createStatement();
			boolean mailStatus = false;

            String userId = request.getParameter("user_id");
            String password = request.getParameter("new_password");
            String name = request.getParameter("name");
            String email = request.getParameter("email");
			
            String message = "";
            if (userId != null) {
                ResultSet rs = s.executeQuery("select userid from users where userid='" + userId + "' or email='" + email + "'");
                if (rs.first()) {
                    message = "User or the email adress you have entered may already exists. Please try again.";
                } else {
                    s.executeUpdate("insert into users (userid, password, name, email, active, admin) values('" + userId + "', '" + password + "', '" + name + "', '" + email + "', 0, 0)");
                    message = "Congratulations! You have successfully signed up. Your sign up request has been sent for approval. Confirmation email will be sent to you by System administrator. Thank you. ";
					
					/*
						Send email
					*/
					
					 String emailContents = "Dear Administrator\n. This is system generated email. \n" + name + ", has just signed up. User is currently not active. You may activate this using admin panel and send confirmation email to: "+email+". User information is as under.\n User Id: "+userId+"\n Password: "+password+" \n\n\nThank you\n\n.PMS Sign Up Service";		
					 ResultSet rs2 = s2.executeQuery("select email from adminemail");
					 
					 String adminEmail="admin@medbill.com";
					 
					 if(rs2.first()){
						 adminEmail= rs2.getString(1);
					 }
					 
					 //out.print(adminEmail);
                     mailStatus = MailUtility.sendMail(adminEmail, "admin@medbill.com", emailContents, "New User Sign Up Request");
					/*
						end email
					*/
					
                }
                rs.close();
            }
            s.close();
            c.close();
            
%>
<html>
    <head>
		<link href="css/medbill.css" rel="stylesheet" type="text/css" />
        <title>Sign Up</title>
        <script language="javascript">		
            function validateForm(){		
        
                if(document.getElementById("user_id").value.length == 0){
                    alert("Enter user id.");
                    document.getElementById("user_id").focus();
                    return false;
                }
        
                var newPass = document.getElementById("new_password");
                var confirmPass = document.getElementById("confirm_password");
        
                if(newPass.value.length > 0)
                {	
                    if(newPass.value!=confirmPass.value){
                        alert("Confirm password does not match.");
                        confirmPass.focus();
                        return false;
                    }
                }else{
                    alert("Enter password.");
                    newPass.focus();
                    return false;
                }	
        
                if(document.getElementById("name").value.length == 0){
                    alert("Enter user name.");
                    document.getElementById("name").focus();
                    return false;
                }	
        
                var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
                var address = document.getElementById("email").value;
                if(reg.test(address) == false) {
                    alert('Invalid Email Address');
                    document.getElementById("email").focus();
                    return false;
                }  	
                return true;	
        
            }
        </script>
    </head>
    
    <body>
	<table width="860" height="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="204" valign="top"><!-- #BeginLibraryItem "/Library/header.lbi" --><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr bgcolor="#fec707">
        <td width="36" height="204">&nbsp;</td>
        <td valign="top"><table width="191" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td height="49">&nbsp;</td>
          </tr>
          <tr>
            <td><a href="index.html"><img src="images/logo.gif" alt="Medbill Solutions" width="191" height="44" border="0"></a></td>
          </tr>
        </table></td>
        <td width="90" valign="top"><table width="85" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td height="29">&nbsp;</td>
          </tr>
          <tr>
            <td><a href="index.html">Home</a></td>
          </tr>
          <tr>
            <td height="8"></td>
          </tr>
          <tr>
            <td><a href="services.html">Services</a></td>
          </tr>
          <tr>
            <td height="8"></td>
          </tr>
          <tr>
            <td height="8"><a href="login.jsp">Log In</a></td>
          </tr>
          <tr>
            <td height="10"></td>
          </tr>
          <tr>
            <td height="16"><a href="contact.html">Contact</a></td>
          </tr>
        </table></td>
        <td width="221"><img src="images/topImage.jpg" width="221" height="204" alt="" /></td>
        <td width="93">&nbsp;</td>
      </tr>
    </table><!-- #EndLibraryItem --></td>
  </tr>
  <tr>
    <td height="30" bgcolor="#1f226f" class="headingBlue">Medical Billing and Claim Processing Professionals</td>
  </tr>
  <tr>
    <td valign="top" bgcolor="#ccae74"><table width="96%" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="470" valign="top" class="txtPageTitle">Sign up</td>
            <td>&nbsp;</td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="560"></td>
              </tr>
              <tr>
                <td class="txtHomeBody"></td>
              </tr>
            </table>
        <form name="signup" onsubmit="return validateForm();" action="sign-up.jsp" method="post">

            <div >
                <table border="0" width="364" cellpadding="0">                    <tr>
                        <td width="360" colspan="2"><font color='red'><%=message%></font></td>
                    </tr>
                    <tr>
                        <td width="120" align="left">&nbsp;</td>
                        <td width="238">&nbsp;</td>
                    </tr>
                    <tr>
                        <td width="120" align="left">User Id:</td>
                        <td width="238"><input name="user_id" id="user_id" size="20" /></td>
                    </tr>
                    <tr>
                        <td width="120" align="left">Password:</td>
                        <td width="238"><input type="password" id="new_password" name="new_password" size="20" /></td>
                    </tr>
                    <tr>
                        <td width="120" align="left">Confirm Password:</td>
                        <td width="238"><input type="password" id="confirm_password" name="confirm_password" size="20" /></td>
                    </tr>
                    <tr>
                        <td width="120" align="left">Name:</td>
                        <td width="238"><input type="text" name="name" id="name" size="20"></td>
                    </tr>
                    <tr>
                        <td width="120" align="left">Email Address:</td>
                        <td width="238"><input type="text" name="email" id="email" size="20"></td>
                    </tr>
                    <tr>
                        <td width="120">&nbsp;</td>
                        <td width="238">&nbsp;</td>
                    </tr>
                    <tr>
                        <td width="120">&nbsp;</td>
                        <td width="238">&nbsp;</td>
                    </tr>
                    <tr>
                        <td width="120">&nbsp;</td>
                        <td width="238"><input type="submit" value="Sign Up"/> 
                        <input type="button" value="Cancel" onclick="location='login.jsp';"/></td>
                    </tr>
                </table>
            </div>
            <p>&nbsp;</p>
        </form>
		
              <br></td>
            <td width="45" valign="top">&nbsp;</td>
            <td valign="top">&nbsp;</td>
          </tr>
        </table></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="62" valign="top"><!-- #BeginLibraryItem "/Library/footer.lbi" --><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td height="4" bgcolor="#c7540f"></td>
      </tr>
      <tr>
        <td height="58" bgcolor="#1e2667">&nbsp;</td>
      </tr>
    </table><!-- #EndLibraryItem --></td>
  </tr>
</table>
    </body>
</html>